<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button v-if="row.exchange_status==0" @click="handle(row)" size="small">处理</el-button>
      <p v-else>无</p>
    </template>
  </list_url>
</template>
<script>
import {ElMessageBox} from "element-plus";
import {_href} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');

    if(in_right_admin('LogScore/export_exchange')) btns['导出'] = function(){
      let param = s94Mjs.param_encode(app.$refs['list_url'].$refs['forms'].form_data());
      window.open(_href("/admin/log_score/export_exchange?"+param), '_blank')
    }
    let forms_config = {
      title_info: {label:'关键词',name:'where[title|info][like]',type:'text_one',value:''},
      phone: {label:'用户手机号',name:'where[user.phone][eq]',type:'text_one',value:''},
      create_time: {label:'兑换时间',name:'where[create_time][between]',config:'Y-M-D',type:'date_between',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', order:true},
      head_img: {name:'head_img', label:'头像', width:'60px', type:'file_img'},
      nickname: {name:'nickname', label:'用户昵称', width:'120px'},
      phone: {name:'phone', label:'电话', width:'120px'},
      score_level: {name:'score_level', label:'积分段位', width:'80px', type:'select_one',config:[]},
      score_goods_head_img: {name:'score_goods_head_img', label:'商品头图', width:'65px', type:'file_img'},
      score_goods_title: {name:'score_goods_title', label:'商品名称', min_width:'120px'},
      exchange_status: {name:'exchange_status', label:'状态', width:'80px', type:'radio_switch',config:'未处理|0,已处理|1'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'兑换时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/log_score/'+(is_del?'recycle':'list_exchange'),
        title: "兑换记录列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    handle(row){
      let can_edit = in_right_admin('LogScore/edit_exchange') && ~[0,row.tenant_id].indexOf(this.admin_config('this_tenant.id'))
      if (!can_edit) return this.tips("权限不足！", 3);
      ElMessageBox.prompt('输入处理备注', '兑换记录处理', {
        inputType: 'textarea',
        confirmButtonText: '提交',
        cancelButtonText: '取消',
        inputValidator: function (v){
          // if (!v) return '备注必填';
          return true;
        },
      }).then((data)=>{
        let url = `/admin/log_score/edit_exchange?id=${row.id}`;
        ajax({url:url, method:'POST', data:{notes:data.value, exchange_status:1}}, (res)=>{
          this.tips("操作成功", 1);
          row.exchange_status = 1;
          return false;
        });
      }).catch(() => {})
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/user_score_level/list?order[id]=asc',cache:true}, (res)=>{
      app.config.list_cols.score_level.config = this.decode_config(res.data.list, {content_key:'title', value_key:'level'});
      return false;
    })
  }
}
</script>
